package com.sz.springboot_jpa.cntroller;

import com.sz.springboot_jpa.entity.User;
import com.sz.springboot_jpa.pojo.ResponseCode;
import com.sz.springboot_jpa.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping("main")
    public String main(){

        return "main";
    }

    @PostMapping
    @ResponseBody
    public ResponseCode loginCheck( User user, HttpSession session){
        ResponseCode code = new ResponseCode();
        User u = userService.findByUsername(user.getUsername());
        //如果为null，则表示用户名
        if(u == null){
            code.setMsg("用户名错误");
            code.setStatus(ResponseCode.FAIL);
            return code;
        }
        //判断密码是否正确
        if(!u.getPassword().equals(user.getPassword())){
            code.setMsg("密码错误");
            code.setStatus(ResponseCode.FAIL);
            return code;
        }
        session.setAttribute("loginUser",u);
        code.setStatus(ResponseCode.SUCCESS);
        code.setMsg("登入成功");
        return code;
    }

}
